##stringtype C
##shortstrings
/****************************************************************
   This file was created automatically by `FlexCat V1.0'
   Do NOT edit by hand!
****************************************************************/

#ifndef %b_CAT_H
#include "%b_CAT.h"
#endif	/*  !%b_CAT_H		 */

#include <string.h>

#ifndef EXEC_MEMORY_H
#include <exec/memory.h>
#endif	/*  !EXEC_MEMORY_H	    */
#ifndef LIBRARIES_IFFPARSE_H
#include <libraries/iffparse.h>
#endif	/*  !LIBRARIES_IFFPARSE_H   */

#ifndef CLIB_IFFPARSE_PROTOS_H
#include <clib/iffparse_protos.h>
#endif	/*  !CLIB_IFFPARSE_PROTOS_H */
#ifndef CLIB_LOCALE_PROTOS_H
#include <clib/locale_protos.h>
#endif	/*  !CLIB_LOCALE_PROTOS_H   */
#ifndef CLIB_DOS_PROTOS_H
#include <clib/dos_protos.h>
#endif	/*  !DOS_PROTOS_H	    */
#ifndef CLIB_EXEC_PROTOS_H
#include <clib/exec_protos.h>
#endif	/*  !EXEC_PROTOS_H	    */
#ifndef CLIB_UTILITY_PROTOS_H
#include <clib/utility_protos.h>
#endif	/*  !CLIB_UTILITY_PROTOS_H  */

#ifdef AZTEC_C
#ifndef __PRAGMAS_LOCALE_LIB_H
#include <pragmas/locale_lib.h>
#endif	/*  !PRAGMAS_LOCALE_LIB_H   */
#ifndef __PRAGMAS_IFFPARSE_LIB_H
#include <pragmas/iffparse_lib.h>
#endif	/*  !PRAGMAS_IFFPARSE_LIB_H */
#ifndef __PRAGMAS_DOS_LIB_H
#include <pragmas/dos_lib.h>
#endif	/*  !PRAGMAS_DOS_LIB_H	    */
#ifndef __PRAGMAS_EXEC_LIB_H
#include <pragmas/exec_lib.h>
#endif	/*  !PRAGMAS_EXEC_LIB_H     */
#ifndef __PRAGMAS_UTILITY_LIB_H
#include <pragmas/utility_lib.h>
#endif	/*  !PRAGMAS_UTILITY_LIB_H  */
#endif	/*  AZTEC_C		    */

static LONG %b_Version = %v;
static const STRPTR %b_BuiltInLanguage = (STRPTR) %l;

struct FC_Type
{   LONG    ID;
    STRPTR  Str;
};

static const struct FC_Type %b_Array [] =
{
    %i, (STRPTR) %s%,
};

static struct Catalog *%b_Catalog = NULL;
static struct FC_Type *%b_OwnCatalog = NULL;
static LONG %b_OwnStrings;
static LONG %b_OwnBytes;

void Open%bCatalog(struct Locale *loc, STRPTR language)
{ LONG tag, tagarg;
  extern struct Library *LocaleBase;
  extern struct Library *IFFParseBase;

  Close%bCatalog();  /*  Not needed if the programmer pairs Open-()
			 and CloseCatalog() right, but does no harm. */
  if (language == NULL)
  { tag = TAG_IGNORE;
  }
  else
  { tag = OC_Language;
    tagarg = (LONG) language;
  }
  if (LocaleBase != NULL  &&  %b_Catalog == NULL)
  { %b_Catalog = OpenCatalog(loc, (STRPTR) "%b.catalog",
			     OC_BuiltInLanguage, %b_BuiltInLanguage,
			     tag, tagarg,
			     OC_Version, %b_Version,
			     TAG_DONE);
  }
  if (LocaleBase == NULL  &&  IFFParseBase != NULL  &&  language != NULL  &&
      Stricmp(language, %b_BuiltInLanguage) != 0)
  { struct IFFHandle *iffhandle;
    char path[128]; /*	Enough to hold 4 path items (dos.library 3.0)  */

    if ((iffhandle = AllocIFF())  !=  NULL)
    { /*  Trying to open the catalog  */
      strcpy(path, "Catalogs");
      AddPart((STRPTR) path, language, sizeof(path));
      AddPart((STRPTR) path, (STRPTR) "%b.catalog", sizeof(path));
      if ((iffhandle->iff_Stream = Open((STRPTR) path, MODE_OLDFILE))
				 ==  NULL)
      { strcpy(path, "Locale:Catalogs");
	AddPart((STRPTR) path, language, sizeof(path));
	AddPart((STRPTR) path, (STRPTR) "%b.catalog", sizeof(path));
	iffhandle->iff_Stream = Open((STRPTR) path, MODE_OLDFILE);
      }

      if (iffhandle->iff_Stream)
      { InitIFFasDOS(iffhandle);
	if (!OpenIFF(iffhandle, IFFF_READ))
	{ if (!PropChunk(iffhandle, MAKE_ID('C','T','L','G'),
			 MAKE_ID('S','T','R','S')))
	  { struct StoredProperty *sp;
	    int error;

	    for (;;)
	    { if ((error = ParseIFF(iffhandle, IFFPARSE_STEP))
			 ==  IFFERR_EOC)
	      { continue;
	      }
	      if (error != 0)
	      { break;
	      }

	      if (sp = FindProp(iffhandle, MAKE_ID('C','T','L','G'),
				MAKE_ID('S','T','R','S')))
	      { LONG *ptr;
		LONG BytesToScan, StrLength;

		/*  First scan: Check the number of strings		*/
		/*  Note that this assumes that the strings are padded	*/
		/*  to a longword boundary!				*/
		%b_OwnBytes = 0;
		%b_OwnStrings = 0;
		BytesToScan = sp->sp_Size;
		ptr = sp->sp_Data;
		while (BytesToScan > 0)
		{ ++%b_OwnStrings;
		  ++ptr;		      /*  Skip ID		*/
		  StrLength = *ptr+1;	      /*  NUL-Byte!		*/
		  %b_OwnBytes += StrLength;
		  ptr += 1+(StrLength+3)/4;   /*  Skip Length and String*/
		  BytesToScan -= 8+((StrLength+3)/4)*4;
		}

		/*  Marginal check: BytesToScan has to be 0!		*/
		if (BytesToScan == 0)
		{ char *cptr;
		  LONG i;

		  if (%b_OwnCatalog = (struct FC_Type *)
		      AllocMem(%b_OwnStrings*sizeof(struct FC_Type)+%b_OwnBytes,
			       MEMF_ANY))
		  { /*	Second scan: Copy the strings and their ID's    */
		    cptr = (char *) &%b_OwnCatalog[%b_OwnStrings];
		    BytesToScan = sp->sp_Size;
		    ptr = sp->sp_Data;
		    i = 0;
		    while (BytesToScan > 0)
		    { %b_OwnCatalog[i].ID = *(ptr++);
		      %b_OwnCatalog[i].Str = (STRPTR) cptr;
		      StrLength = *ptr+1;     /*  NUL-Byte!		*/
		      ptr++;
		      strncpy(cptr, (char *) ptr, StrLength);
					/*  Not more, not less bytes!	*/
		      cptr+=StrLength;
		      ptr += (StrLength+3)/4;
		      BytesToScan -= 8+((StrLength+3)/4)*4;
		      ++i;
		    }
		    break;
		  }
		}
	      }
	    }
	  }
	  CloseIFF(iffhandle);
	}
	Close(iffhandle->iff_Stream);
      }

      FreeIFF(iffhandle);
    }
  }
}


void Close%bCatalog(void)
{ if (LocaleBase != NULL)
  { CloseCatalog(%b_Catalog);
  }
  %b_Catalog = NULL;
  if (%b_OwnCatalog != NULL)
  { FreeMem(%b_OwnCatalog,
	    %b_OwnStrings*sizeof(struct FC_Type)+%b_OwnBytes);
    %b_OwnCatalog = NULL;
  }
}


STRPTR Get%bString(LONG strnum)
{ STRPTR defaultstr = NULL;
  LONG i;

  for (i = 0;  i < sizeof(%b_Array)/sizeof(struct FC_Type);  i++)
    { if (%b_Array[i].ID == strnum)
      { defaultstr = %b_Array[i].Str;
	break;
      }
    }
  if (%b_Catalog == NULL)
  { if (%b_OwnCatalog != NULL)
    { for (i = 0;  i < %b_OwnStrings;  i++)
       { if (%b_OwnCatalog[i].ID == strnum)
	 { return(%b_OwnCatalog[i].Str);
	 }
       }
    }
    return(defaultstr);
  }
  return(GetCatalogStr(%b_Catalog, strnum, defaultstr));
}
